#include <stdio.h>
#include "mysql.h"
#define TIP "127.0.0.1"
#include "zvector.h"

void showtablesdemo(MYSQL * conn
	, MYSQL_RES * res
	, MYSQL_ROW * row)
{
	if (mysql_query(conn, "show tables"))
	{
		fprintf(stderr, "err: %s\n", mysql_error(conn));
		return;
	}
	res = mysql_use_result(conn);
	while((row = mysql_fetch_row(res)) != NULL)
	{
		printf("%s\n", row[0]);
	}
	mysql_free_result(res);
}
void selectfromtuserdemo(MYSQL * conn
	, MYSQL_RES * res
	, MYSQL_ROW * row)
{
	if (mysql_query(conn, "select * From TUser;"))
	{
		fprintf(stderr, "err: %s\n", mysql_error(conn));
		return;
	}
	res = mysql_use_result(conn);
	while((row = mysql_fetch_row(res)) != NULL)
	{
		printf("%s -- %s - %s\n", row[0], row[1], row[2]);
	}
	mysql_free_result(res);
}

void parameterquerydemo(MYSQL * conn
	, MYSQL_RES * res
	, MYSQL_ROW * row)
{
	//after conn
	//1. STMT
	MYSQL_STMT * stmt;
	MYSQL_BIND bind[2];
	memset(bind, 0, sizeof(bind));
	stmt = mysql_stmt_init(conn);
	char * sql = "insert into TUser(TU_Guid, TU_UserName, TU_Password) values(uuid(), ?, ?)";
	if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		return;
	}
	char inname[255] = {0};
	char inpwd[255] = {0};
	strcpy(inname, "zjr");
	strcpy(inpwd, "admi");
	bind[0].buffer_type = MYSQL_TYPE_STRING;
	bind[0].buffer = inname;
	bind[0].buffer_length = strlen(inname);
	bind[1].buffer_type = MYSQL_TYPE_STRING;
	bind[1].buffer = inpwd;
	bind[1].buffer_length = strlen(inpwd);
	if(mysql_stmt_bind_param(stmt, bind))
	{
		return;
	}
	if(mysql_stmt_execute(stmt))
	{
		return;
	}
	mysql_stmt_close(stmt);


}

void parameterquerydemo2(MYSQL * conn
	, MYSQL_RES * res
	, MYSQL_ROW * row)
{
	//after real_conn before close

	//1. create stmt ptr
	MYSQL_STMT * stmt = mysql_stmt_init(conn);
	char * sql = "select * From TUser where TU_UserName = ?";

	//2. stmt_prepare
	if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		printf("prepare err!\n");
		return;
	}

	//3. vars
	char guid[36] = {0};
	char uname[255] = {0};
	char upwd[255] = {0};

	char inuname[255] = {0};
	strcpy(inuname, "guest");

	MYSQL_BIND params[3];
	memset(params, 0, sizeof(params));
	params[0].buffer_type = MYSQL_TYPE_STRING;
	params[0].buffer = guid;
	params[0].buffer_length = sizeof(guid);
	params[1].buffer_type = MYSQL_TYPE_STRING;
	params[1].buffer = uname;
	params[1].buffer_length = sizeof(uname);
	params[2].buffer_type = MYSQL_TYPE_STRING;
	params[2].buffer = upwd;
	params[2].buffer_length = sizeof(upwd);

	MYSQL_BIND inparams[1];
	memset(inparams, 0, sizeof(inparams));
	inparams[0].buffer_type = MYSQL_TYPE_STRING;
	inparams[0].buffer = inuname;
	inparams[0].buffer_length = strlen(inuname);

	//4.bindpara and bind result
	mysql_stmt_bind_param(stmt, inparams);
	mysql_stmt_bind_result(stmt, params);
	mysql_stmt_execute(stmt);
	mysql_stmt_store_result(stmt);

	while (mysql_stmt_fetch(stmt) == 0)
	{
		printf("%s, %s, %s", guid, uname, upwd);
	}

	mysql_stmt_close(stmt);
}


int main()
{
	MYSQL * conn;
	MYSQL_RES * res;
	MYSQL_ROW * row;

	// init ptr of conn
	conn = mysql_init(NULL);

	if(conn == NULL)
	{
		printf("mysql_init failed\n");
		return -1;
	}

	conn = mysql_real_connect(conn, TIP, "root", "9904wcnm", "DBGameServer", 0, NULL, 0);
	if(conn)
	{
		printf("连接成功\n");

		//showtablesdemo(conn, res, row);
		//selectfromtuserdemo(conn, res, row);
		//parameterquerydemo(conn, res, row);
		parameterquerydemo2(conn, res, row);
	}
	else
	{
		printf("连接失败\n");
	}
	mysql_close(conn);

	printf("ok\n");
	return 0;
}
